<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Weixin\Controller;
use OT\DataDictionary;

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class AuthenticationController extends WeixinController {
     protected function _initialize(){
        require_once('ThinkPHP/Library/Vendor/api_payment/llpaypayment.php');
        require_once('ThinkPHP/Library/Vendor/OCR_API.php');


        /* 读取站点配置 */
        $config = api('Config/lists');
        C($config); //添加配置
        if(!C('WEB_SITE_CLOSE')){
            $this->error('站点已经关闭，请稍后访问~');
        }
        $options = array(
            'token'     => $this->token,   // 填写你设定的key
            'appid'     => $this->appid,    // 填写高级调用功能的appid
            'appsecret' => $this->appsecret // 填写高级调用功能的密钥
        );
        $this->weixin = new \Common\Extend\Wechat\Wechat($options);
        $weixin = cookie('WeixinLogin');
        if(empty($weixin)){
            $this->oauth();
        }else{
            $this->uid          = $weixin['uid'];
            $this->access_token = $weixin['access_token'];
            $this->openid       = $weixin['openid'];
        }
        $where['id'] =$this->uid;
        $count = D('ucenter_member')->where($where)->getField('mobile');
        if(empty($count)){
            $this->redirect('User/mobile');
        }
        
    }

    public function index(){

        C('WEB_SITE_TITLE','我的认证');
         $realname = M('Realname')->where('uid='.$this->uid)->find();
         $chsi = D('Chsi')->where('uid='.$this->uid)->find();
         $bankcard = M('BankCard')->where('uid='.$this->uid)->find();
         $mobileinfo = M('Mobileinfo')->where('uid='.$this->uid)->find();
         $member = M('Member')->where('uid='.$this->uid)->find();


         
         
         $this->assign('realname',$realname);
         $this->assign('chsi',$chsi);
         $this->assign('bankcard',$bankcard);
         $this->assign('mobileinfo',$mobileinfo);
         

        $this->display();
    }
	//系统首页
    public function xuexin(){
       if(IS_POST){
          
            $username = I('post.username');
            $password = I('post.password');
            if(empty($username)){
                ob_clean();
                $this->ajaxReturn(['info'=>'学信账号不能为空']);
            }elseif(empty($password)){
                ob_clean();
                $this->ajaxReturn(['info'=>'学信密码不能为空']);
            }
           session('APPToken','');
            $api = new \Common\Extend\Api\ApiClient();
            $api = $api->credit();
            $api->method    =   'api.education.get';
            $api->username  =   $username;
            $api->password  =   base64_encode($password);
            $data           =   $api->Send('post','json')->data();
            if($data['code']=='0000'){
                session('APPToken',$data['token']);
                ob_clean();
                $this->ajaxReturn(['info'=>'学信网授权成功，下一步完成认证!']);
            }elseif($data['code']=='0010'){
                ob_clean();
                $this->ajaxReturn(['status'=>false,'type'=>'education','token'=>$data['token'],'info'=>'正在受权中']);
            }else{
                ob_clean();
                $this->ajaxReturn(['info'=>$data['msg']]);
            }
        }else{
             //如果已认证成功跳过
            $vo = M('Realname')->where('uid='.$this->uid)->find();
            if(empty($vo)){
                $this->redirect('Authentication/OCR');
            }
           $count = D('Chsi')->where('status=2 and uid='.$this->uid)->count();
            if(!empty($count)){
                $this->redirect('xuexininfo');
            }
            C('WEB_SITE_TITLE','学信认证');
            $this->display();
        }
    }

    public function xuexininfo(){
        $Token =  session('APPToken');
        $model =  D('Chsi');
        $info = $model->where('uid='.$this->uid)->find();
        //查出绑定的身份证
        if(IS_POST){
            $vo = M('Realname')->where('uid='.$this->uid)->find();
            if(empty($vo)){
                $this->redirect('Authentication/OCR');
            }
            if($info['name']==$vo['realname'] && $info['identityNo']==$vo['identityNo']){
                $model->where('uid='.$this->uid)->save(['status'=>2]);
                session('APPToken','');
                ob_clean();
                $this->ajaxReturn(['info'=>'验证通过!','url'=>U('index')]);
            }else{
                ob_clean();
                $this->ajaxReturn(['info'=>'验证失败,身份认证与学信信息不一致']);
            }
        }else{
            if(empty($info)){
                $volist = $model->UpdateXueXin($this->uid,$Token);//添加
            }elseif($info['status']!=2){
                $volist = $model->UpdateXueXin($this->uid,$Token,true);//更新
            }else{
                $volist = $info;//添加
            } //print_r($volist);exit;
            C('WEB_SITE_TITLE','学信信息');
            $this->assign('info',$volist);
            $this->display();
        }
    }

    /*
     * realname
     */
    public function realname(){
         $model = M('Realname');
        if(IS_POST){
            $hold_pic      = I('post.hold_pic');
            if(empty($hold_pic)){
                ob_clean();
                $this->AjaxReturn(['info'=>'请上传手持身份证']);
            }
            $data = [
                'hold_pic'     =>$hold_pic,
                'status'       =>2,
            ];
            $result=$model->where('uid='.$this->uid)->save($data);  //echo $model->getLastSql();exit;

            if($result==1){
                //保存用户真名字
                
                ob_clean();
                $this->AjaxReturn(['info'=>'操作成功']);
            }else{
                ob_clean();
                D('Realname')->where('uid='.$this->uid)->delete();
                $this->AjaxReturn(['info'=>'操作失败，请重新上传！']);
            }
        }else{
            $timestamp      = time();
            $noncestr       = $this->weixin->generateNonceStr();
               $jsapi_ticket =  cookie('jsapi_ticket');  
               if(empty($jsapi_ticket)){
                    $jsapi_ticket   = $this->weixin->getticket();
                    cookie('jsapi_ticket',$jsapi_ticket,3600);  
               }
            
            $config = [
                'noncestr'      =>$noncestr,
                'jsapi_ticket'  =>$jsapi_ticket,
                'timestamp'     =>$timestamp,
                'url'           =>get_url(),
            ];
            $i = 0;
            $stringToBeSigned='';
            ksort($config);
            foreach ($config as $k => $v) {
                    if ($i == 0) {
                        $stringToBeSigned .= "$k" . "=" . "$v";
                    } else {
                        $stringToBeSigned .= "&" . "$k" . "=" . "$v";
                    }
                    $i++;
            }
            $signature = sha1($stringToBeSigned);
            $weixinconfig['nonceStr']   =  $noncestr;
            $weixinconfig['signature']  = $signature;
            $weixinconfig['timestamp']  = $timestamp;
            $weixinconfig['appId']      = $this->appid;
            $this->assign('weixinconfig',$weixinconfig);
            $map = [
                'uid'=>$this->uid,
            ];

            $info = $model->where($map)->find();
            // print_r($info);die;
            $this->assign('hold_pic',$info['hold_pic']);
            $this->assign('info',$info);
            C('WEB_SITE_TITLE','身份认证');
            $this->display();
        }
    }
    public function notify(){
         file_get_contents("php://input");
    }

// 卡BIN查询
      public function cardbin(){
        $card_no=I('post.card_no');
        $r=Bankcardbin($card_no);
        $r=json_decode($r,true);
        ob_clean();
        print_r($r['bank_name']);die;
      }

    /*银行卡号*/
    public function yinhang(){
        // require_once('ThinkPHP/Library/Vendor/api_payment/llpaypayment.php');
        $model =  M('BankCard');
        if(IS_POST){
           $bank_name    =  I('post.bank_name');
           
           $name        =  I('post.name');
           $card_number =  I('post.card_number');
           $mobile      =  I('post.mobile');
           if(empty($name)){
               $this->error('请输入开户名字');
           }elseif(empty($card_number)){
               $this->error('请输入银行卡号');
           }elseif(empty($mobile)){
               $this->error('请输入预留号码');
           }

           if($bank_name==='中国工商银行'){
                $openbank=1;
            }else if($bank_name==='中国银行'){
                $openbank=2;
            }else if($bank_name==='中国建设银行'){
                $openbank=3;
            }else if($bank_name==='中国农业银行'){
                $openbank=4;
            }else if($bank_name==='交通银行'){
                $openbank=5;
            }else if($bank_name==='招商银行'){
                $openbank=6;
            }else if($bank_name==='中信银行'){
                $openbank=7;
            }else if($bank_name=='光大银行'){
                $openbank=8;
            }else if($bank_name==='民生银行'){
                $openbank=9;
            }else if($bank_name==='兴业银行'){
                $openbank=12;
            }else if($bank_name==='中国邮政储蓄银行'){
                $openbank=14;
            }else{
                $openbank=999;
            }


            $one=D('BankCard')->where('uid='.$this->uid)->find();
            if(!empty($one)){
                $this->error('该用户已绑定过银行卡，如有误，请联系客服人员');
            }
            $data=[
                'uid'           =>  $this->uid,
                'bank_name'     =>  $bank_name,
                'province'      =>  $province,
                'city'          =>  $city,
                'subbranch'     =>  $subbranch,
                'name'          =>  $name,
                'card_number'   =>  $card_number,
                'mobile'        =>  $mobile,
                'create_time'   =>  time(),
                'status'        =>  2,
                'openbank'      =>$openbank,
            ];

            //银行卡签约  $uid,$no_order,$dt_order,$id_no,$acct_name,$card_no,$bind_mob,$notify_url
            
            
            $uid       = $this->uid;
            $real      = D('realname')->where('uid='.$uid)->find();
            $res        = D('member')->where('uid='.$uid)->find();
            $reg_time  = $res['reg_time'];
            $no_order  = date("YmdHis",time()).rand(100000,999999);//用户在商户系统中的唯一辨识
            
            $dt_order   =date("YmdHis",time());
            $id_no      =$real['identityNo'] ;//身份证号
            $acct_name  =$real['realname'];
            $card_no    =$card_number;
            $bind_mob   =$mobile;
            $notify_url ='http://weixin.jcsmkj.cn/index.php?m=weixin&c=authentication&a=/';
            $re=Signcreatebill($uid,$no_order,$dt_order,$id_no,$acct_name,$card_no,$bind_mob,$notify_url,$reg_time);

            $r=json_decode($re,true);

            if($r['ret_code']=='0000'){ //绑卡申请成功
                $data['no_order']=$no_order;
                $data['token']=$r['token'];
                //先不写入数据库  因为签约申请可能不成功或token失效
                // $trl   = $model->add($data);  //写入数据库
                    //银行卡签约结果验证参数 
                    $parameter = array(
                        "api_version"       =>'1.0',
                        'user_id'           =>$uid,
                        "oid_partner"       =>$r['oid_partner'],
                        "token"             =>$r['token'],
                        "no_order"          =>$no_order,
                        "flag_chnl"         =>'3',
                                
                    );
                    // $parameter=json_encode($parameter);
                    $trl=['status'=>true,'info'=>$parameter,'url'=>U('authentication/index'),'data'=>$data];
                
            }else{ //不成功返回不成功的信息
                $ret_msg=$r['ret_msg'];
                $trl=['info'=>$ret_msg,'status'=>false];
            }
        
            ob_clean();
            $this->AjaxReturn($trl);
           

        }else{
            $realname = M('Realname')->where("uid={$this->uid}")->find();
            if($realname['status']!=2){
                $this->redirect('Authentication/OCR');
            }
            $model =  M('BankCard');

            $info = $model->where("uid={$this->uid}")->find();

            
            
            $mobile = M('Member')->where("uid={$this->uid}")->getField('mobile');
            $this->assign('mobile',$mobile);
            

            $this->assign('info',$info);
            $this->assign('realname',$realname['realname']);
            C('WEB_SITE_TITLE','银行卡号');
            $this->display();
        }
    }
    //验证签约成功后返回一个数据 必存
    public function yinhang2(){
        if(IS_POST){
            $trl= M('BankCard')->add(I('post.'));
            if($trl){
                echo 1;die;
            }else{
                echo 0;die;
            }
        }
    }
    //银行卡解约 https: // traderapi.lianlianpay.com/bankcardunbind.htm
    public function bankcardunbind(){
        require_once('ThinkPHP/Library/Vendor/api_payment/llpaypayment.php');


        // 存在多个UID下的同一张卡的时候？？？？？找创建时间最新的一笔
        $data['card_number']=I('post.card_number');
        $data['uid']=$this->uid;


        $res=D('Loan')->where($data)->order('`create_time desc`')->find();
        if($res['status']==1){
            $trl=['info'=>'您的借款订单已创建，请不要解除绑卡，以免放款失败！','status'=>flase];
        }else if($res['status']==2){
            $trl=['info'=>'您的借款订单已处于审核状态，请不要解除绑卡，以免放款失败！','status'=>flase];
        }else if($res['status']==4){
            $trl=['info'=>'您的借款订单已处于待打款状态，请不要解除绑卡，以免放款失败！','status'=>flase];
        }else if($res['status']==5){
            $trl=['info'=>'您的借款还未还清，请先还款再进行操作！！','status'=>flase];
        }else{
            $info=D('BankCard')->where($data)->find();

            $no_agree=$info['no_agree'];
            //调用解除绑定的方法
            $result=Bankcardunbind($this->uid,$no_agree);
            $result=json_decode($result,true);
            if($result['ret_code']=='0000'){
                //解绑之后删除数据库该行          因暂不支持绑定多张，固可以这么写！
                D('BankCard')->where('uid='.$this->uid)->delete();
                $trl=['info'=>'解绑成功！','status'=>true];

            }else{
                $trl=['info'=>$result['ret_msg'],'status'=>false];
            }
        }

        
        ob_clean();
        $this->AjaxReturn($trl);


    }


     // 校验手机号码归属地+  是否属于白名单
    function check_mobile(){

        echo 1;die;

        // $mobile=I('post.mobile');

        // $api = new \Common\Extend\Api\ApiClient();
        // $api = $api->mobile();
        // $api->method      = 'mobile/v1/location';
        // $api->mobileNo    = $mobile; //
        // $data=$api->Send('post','json')->data(); //19e520ae479a473d8dadb8e13d40bd81 realname identityNo
        // if($data['data']['province']=='广西'){
        //     echo 1;die;
        // }else{
        //     $one=D('Deny')->where('mobile='.$mobile)->find();
        //     if(!empty($one)&&$one['status']==1){
        //         echo 1;die;
        //     }
            
        //     echo 0;die;
        // }
    }

    /*
     * mobile
     *//*手机认证*/
    public function mobile(){
        // echo "站点维护中";die;
        if(IS_POST){

            $vo = M('Realname')->where('uid='.$this->uid)->find();
           
            $username   = I('post.username');
            $password   = I('post.password');
            if(empty($username)){
                ob_clean();
                $this->ajaxReturn(['info'=>'绑定号码不能为空']);
            }elseif(empty($password)){
                ob_clean();
                $this->ajaxReturn(['info'=>'服务密码不能为空']);
            }



            // 手机认证缴费
            $one=D('MobileMoney')->where('uid='.$this->uid)->find();

            if(empty($one)){
                ob_clean();
                $this->ajaxReturn(['info'=>27]);
            }

                session('APPToken','');
                $api = new \Common\Extend\Api\ApiClient();
                $api = $api->mobile();
                $api->method            = 'mobile_report/v1/task';
                $api->identityCardNo    = $vo['identityNo']; //查询用户的身份证号
                $api->identityName      = $vo['realname']; //查询用户的真实姓名
                $api->otherInfo         = $vo['identityNo']; 
                $api->username          = $username;
                $api->password          = base64_encode($password);
                $data=$api->Send('post','json')->data(); //19e520ae479a473d8dadb8e13d40bd81 realname identityNo

                // ob_clean();
                // $this->ajaxReturn(['info'=>$data]);



                if($data['code']=='0000'){
                    session('APPToken',$data['token']);
                    ob_clean();
                    $this->ajaxReturn(['info'=>'授权成功，下一步完成认证!']);
                }elseif($data['code']=='0010'){   //task  0010  受理成功
                    ob_clean();
                    $this->ajaxReturn(['status'=>false,'token'=>$data['token'],'info'=>'正在受权中']);
                }elseif($data['code']=='0006'){
                    ob_clean();
                    $this->ajaxReturn(['status'=>false,'token'=>$data['token'],'captcha'=>true,'afresh'=>false,'info'=>'请输入验证码']);
                }else{
                    ob_clean();
                    $this->ajaxReturn(['info'=>$data['msg']]);
                }
        }else{





            // 验证之前的资料有无完成
            $vo = M('Realname')->where('uid='.$this->uid)->find();
            if($vo['status']!=2){
                $this->redirect('Authentication/OCR');
            }


            $bank = M('BankCard')->where('uid='.$this->uid)->find();
            if(empty($bank)){
                $this->redirect('Authentication/yinhang');
            }


            $mobileinfo     =  M('Mobileinfo');
            $info           = $mobileinfo->where('uid='.$this->uid)->find();
            if(!empty($info)){
                if($info['status']==2 || $info['status']==1 || $info['status']==0){
                     $this->redirect('Authentication/mobileinfo2');
                }elseif($info['status']==-1){



                    // 更改为自动获取数据
                    // $trl =  D('Mobileinfo')->automatic($id);
                    $trl = D('Mobileinfo')->automatic($this->uid);  //获取
                    // if($trl==true){

                    // }else{
                    // 获取不到才显示mobileinfo

                // }





                    $time = time() - 7200;
                    if($info['updatetime']>$time){
                        $this->redirect('mobileinfo');
                    }
                }
            }
            $mobile = M('Member')->where("uid={$this->uid}")->getField('mobile');
            $this->assign('mobile',$mobile);
            C('WEB_SITE_TITLE','手机认证');
            $this->display();
        }
    }


public function order_id(){
    //算出订单ID
$id = date('Ymd').substr(time(),-5).substr(microtime(),2,2).rand(100,999);
if($this->where("id={$id}")->count())  //订单号已存在
    return $this->order_id();
else
    return $id;
}

public function alipay(){


        $vo = D('MobileMoney')->where('uid='.$this->uid)->find();
        if($vo['money']!=0){
           $this->error('订单不需要付款款！');
        }else{
            // 交易单号不再是借款id  自动生成一个
            $id=$this->order_id;
                $remark = '订单号'.$id;
                $title  = '订单号'.$id;
                $money=27;
                $type = 3;
            $data = D('Trade')->CreateTrade($this->uid,$id,$title,1,$type,$money,$remark);
            // print_r($data);die;
            $this->redirect('User/alipay?id='.$data['id']);
        }
    
}

    public function input(){        
        $token     = I('post.token');
        $captcha   = I('post.captcha');

        session('APPToken','');
        if(!empty($token) && !empty($captcha)){    //token和验证码都不为空，才进入input
            $api = new \Common\Extend\Api\ApiClient();
            $api = $api->mobile();

            $api->method   = 'mobile_report/v1/task/input';
            $api->token    = $token;
            $api->input    = $captcha;
            $data = $api->Send('post','json')->data();
            if($data['code']=='0009'){
                ob_clean();
                $this->ajaxReturn(['status'=>false,'token'=>$data['token'],'info'=>'正在受权中']);
            }else{
                ob_clean();
                $this->ajaxReturn(['info'=>$data['msg']]);
            }
        }
    }

    public function mobileinfo(){
        // echo "站点维护中";die;


        
        $token              = session('APPToken');
        $mobileinfo         =  M('Mobileinfo');
        $count              = $mobileinfo->where('uid='.$this->uid)->count();
        $data = [
          'token'       =>$token,
          'uid'         =>$this->uid,
          'mobile '     =>'',
          'name'        =>'',
          'idCard'      =>'',
          'address'     =>'',
          'register'    =>'',
          'content'     =>'',
          'status'      =>-1,
          'updatetime'  =>time(),//两个小时查不到数据自动删除去
        ];
        if(!empty($token)){
            if(empty($count)){
                $mobileinfo->add($data);
            }else{
                $mobileinfo->where('uid='.$this->uid)->save($data);
            }
        }
        session('APPToken','');
        $this->display();
    }
    public function mobileinfo2(){
        $mobileinfo         =  M('Mobileinfo');
        $info               = $mobileinfo->where('uid='.$this->uid)->find();
        C('WEB_SITE_TITLE','认证成功');
        $this->assign('info',$info);
        $this->display('Authentication/mobileinfo2');
    }
   

    public function OCR(){
        // echo 0;die;
        $model = M('Realname');
        if(IS_POST){
            $front_pic      = I('post.front_pic');
            $opposite_pic   = I('post.opposite_pic');
            $uid=$this->uid;
            $data = [
                'uid'       =>$uid,
                'front_pic' =>$front_pic,
                'opposite_pic'  =>$opposite_pic,
            ];
            


            $id = $model->add($data);  //echo $model->getLastSql();exit;
            if($id>0){
                $front_path=$this->get_weixin_pic($front_pic,'Realname',"uid=".$this->uid,'front_pic');
                $opposite_path=$this->get_weixin_pic($opposite_pic,'Realname',"uid=".$this->uid,'opposite_pic');
                // 正面
                $front=verifyIdCard($front_path,1);
                $front=json_decode($front,true);
                
                if($front['result']['success']==1){
                    
                        $datas['realname']       =$front['data']['id_name'];
                        $datas['identityNo']     =$front['data']['id_number'];
                        $datas['nation']         =$front['data']['nation'];
                        $datas['gender']         =$front['data']['gender'];
                        $datas['address']        =$front['data']['address'];



                        // 反面
                        $oppsite=verifyIdCard($opposite_path,2);
                        $oppsite=json_decode($oppsite,true);
                        ob_clean();
                            
                    
                    if($oppsite['result']['success']==1){
                        $datas['issuing_authority']      =$oppsite['data']['issuing_authority'];
                        $datas['validity_period']        =$oppsite['data']['validity_period'];


                        // $birth=substr($datas['identityNo'], 6,8);
                        // $date=date("Ymd");
                        if($front['data']['age']<23){
                            print_r("未满23周岁禁止借款");die;
                        }



                        
                        $set=$model->where("identityNo='$datas[identityNo]'")->find();
                        // print_r($model->getLastSql());die;
                        if(empty($set)){   //该身份证号未存在
                            $model->where('uid='.$uid)->save($datas);
                            M('Member')->where("uid={$this->uid}")->save(['realname'=>$datas['realname']]);
                            print_r('信息识别成功！');die;
                        }else{
                            // 身份证号已存在则说明该用户已用另一个微信号实名过
                            $model->where('uid='.$uid)->delete();
                            print_r('该身份证已由另一微信号认证，如有疑问，请联系客服核实');die;
                        }



                        
                    }else{
                        $model->where('uid='.$uid)->delete();
                        print_r($oppsite['result']['message']);die;
                    }
                    
                }else{
                    $model->where('uid='.$uid)->delete();
                    print_r($front['result']['message']);die;
                }
            
            }else{
                print_r('资料保存失败！');die;
            }


        }else{
            $timestamp      = time();
            $noncestr       = $this->weixin->generateNonceStr();
               $jsapi_ticket =  cookie('jsapi_ticket');  
               if(empty($jsapi_ticket)){
                    $jsapi_ticket   = $this->weixin->getticket();
                    cookie('jsapi_ticket',$jsapi_ticket,3600);  
               }
            
            $config = [
                'noncestr'      =>$noncestr,
                'jsapi_ticket'  =>$jsapi_ticket,
                'timestamp'     =>$timestamp,
                'url'           =>get_url(),
            ];
            $i = 0;
            $stringToBeSigned='';
            ksort($config);
            foreach ($config as $k => $v) {
                    if ($i == 0) {
                        $stringToBeSigned .= "$k" . "=" . "$v";
                    } else {
                        $stringToBeSigned .= "&" . "$k" . "=" . "$v";
                    }
                    $i++;
            }
            $signature = sha1($stringToBeSigned);
            $weixinconfig['nonceStr']   =  $noncestr;
            $weixinconfig['signature']  = $signature;
            $weixinconfig['timestamp']  = $timestamp;
            $weixinconfig['appId']      = $this->appid;
            $this->assign('weixinconfig',$weixinconfig);
            $map = [
                'uid'=>$this->uid,
            ];


            $info = $model->where($map)->find();
           if(!empty($info)&&empty($info['hold_pic'])){
                $this->assign('info',$info);
                C('WEB_SITE_TITLE','身份认证');
                $this->display('realname');
            }else{
                $this->assign('info',$info);
                C('WEB_SITE_TITLE','身份认证');
                $this->display('OCR');
            }
        
        }
            
    }

    /*
 *     微信获取图片
 *
 */
function get_weixin_pic($media_id,$model=false,$where=false,$field=false){
    $pathinfo = pathinfo($media_id);
    if(!empty($pathinfo['extension'])){
        return $media_id;
    }
    $options = array(
        'token'     => 'Xxshtong',   // 填写你设定的key
        'appid'     => 'wxf8cc7ff487feb1d4',    // 填写高级调用功能的appid
        'appsecret' => 'b7f71b894899a6dcaedd816c4e497003' // 填写高级调用功能的密钥
    );
    $weixin = new \Common\Extend\Wechat\Wechat($options);
    $url = $weixin->downloadPic($media_id);
    if(!empty($model)){
        if(!empty($url)){
            M($model)->where($where)->save([$field=>$url]);
        }
    }
    return $url;
}


}